package com.project.frba.bifront.client.view.dashboard;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.DashboardServices;
import com.project.frba.bifront.client.rpc.DashboardServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class DashboardMainPanel extends Panel{
	
	private Panel topPanel = new Panel();
	private Panel centerPanel = new Panel();
	private ComboBox cbUserDashboards = new ComboBox();
	private Map<String, Dashboard> userDashboards = new HashMap<String, Dashboard>();
	private String selectedKey = null;
	private Integer selectedType = null;
	private Button btAddDashboard = new Button("Nuevo Tablero");
	
	
	public DashboardMainPanel(){
		getDashboards();
	}
	
	public void loadDashboard(){
		//topPanel
		cbUserDashboards.setDisplayField("name");
		cbUserDashboards.setMode(ComboBox.LOCAL);
		cbUserDashboards.setTriggerAction(ComboBox.ALL); 
		cbUserDashboards.setEditable(false);
		cbUserDashboards.setWidth("250px");
		cbUserDashboards.setBlankText("Seleccione un tablero de comando..");
		topPanel.setLayout(new HorizontalLayout(3));
		topPanel.setWidth(1280);
		topPanel.setHeight(30);
		topPanel.add(cbUserDashboards);
		topPanel.add(btAddDashboard);
		topPanel.setPaddings(4);
		
		centerPanel.setHeight(577);

		//this
		this.setTitle("Tablero de control");
		this.add(topPanel);
		this.add(centerPanel);
		loadActions();
	}
	
	private void loadActions() {
		cbUserDashboards.addListener(new ComboBoxListenerAdapter(){
			public void onSelect(ComboBox comboBox, Record record, int index) {
				selectedType = record.getAsInteger("type");
				selectedKey = record.getAsString("key");
				refreshDashboard();
			}
		});
		
		final DashboardMainPanel me = this;
		btAddDashboard.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				new CreationDashboardWindow(me);
			}
		});
		
		
		
	}
	
	public void refreshDashboard(){
		centerPanel.removeAll();
		if(Dashboard.DUAL.equals(selectedType) && selectedKey != null){
			centerPanel.add(new DualDashboardPanel(userDashboards.get(selectedKey)));
		} else if(Dashboard.QUAD.equals(selectedType) && selectedKey != null) {
			centerPanel.add(new QuadDashboardPanel(userDashboards.get(selectedKey)));
		}
		centerPanel.doLayout();
	}

	public void getDashboards(){
		AsyncCallback<List<Dashboard>> cbMyDashboards = new AsyncCallback<List<Dashboard>>(){
			public void onSuccess(List<Dashboard> dashboards) {
				FieldDef[] fieldDefs = new FieldDef[]{
						new StringFieldDef("id"), 
						new StringFieldDef("name"), 
						new IntegerFieldDef("type"),
						new StringFieldDef("key")
						};
				
				RecordDef recordDef = new RecordDef(fieldDefs);
				Store store = new Store(recordDef);
				cbUserDashboards.setStore(store);
				cbUserDashboards.setEmptyText("Seleccione un tablero...");
				for (Dashboard dashboard : dashboards) {
					store.add(recordDef.createRecord(new Object[]{
							dashboard.getId().toString(),
							dashboard.getName(), 
							dashboard.getType(),
							dashboard.getKey()
							}));
					store.commitChanges();
					userDashboards.put(dashboard.getKey(), dashboard);
				}
				if(dashboards.get(0) != null){
					cbUserDashboards.setValue(dashboards.get(0).getName());
					Dashboard firstDashboard = dashboards.get(0);
					selectedKey = firstDashboard.getKey();
					selectedType = firstDashboard.getType();
				}
				refreshDashboard();
				
			}
			
			public void onFailure(Throwable caught) {
				new MessageWindow("Error al cargar los tableros de comando para el usuario.");
				
			}
		};
		DashboardServicesAsync dashboardServices = (DashboardServicesAsync)GWT.create(DashboardServices.class);
		dashboardServices.getMyDashboards(cbMyDashboards);
	}

	
}
